package br.com.cast.scc.pages.veiculo;



import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.feedback.ExactLevelFeedbackMessageFilter;
import org.apache.wicket.feedback.FeedbackMessage;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import br.com.cast.scc.enums.StatusRegistroAtivo;
import br.com.cast.scc.model.DominioTipoVeiculo;
import br.com.cast.scc.model.Veiculo;
import br.com.cast.scc.pages.filtros.FiltroPesquisaVeiculos;
import br.com.cast.scc.service.TipoVeiculoService;
import br.com.cast.scc.service.VeiculoService;

public class CadastrarVeiculoForm extends Form<Veiculo> {
	
	private WebMarkupContainer panelCamposTerrestre;
	private WebMarkupContainer panelCamposFluviais;
	private static final long serialVersionUID = 1L;

	@SpringBean(name="tipoVeiculoServiceImpl")
	private TipoVeiculoService tipoveiculoService;
	
	@SpringBean(name="veiculoServiceImpl")
	private VeiculoService veiculoService;
	

	public CadastrarVeiculoForm(String id) {
		super(id, new CompoundPropertyModel<Veiculo>(new Veiculo()));
		
		add(new FeedbackPanel("Erro",new ExactLevelFeedbackMessageFilter(FeedbackMessage.ERROR)).setOutputMarkupId(true));
		add(new FeedbackPanel("Sucesso", new ExactLevelFeedbackMessageFilter(FeedbackMessage.SUCCESS)).setOutputMarkupId(true));
		
		criaVoltar();
		
		criaPanelTerrestre();
		
		criaPanelFluvial();
		
		criaNuEmbarcacao();
		
		criaRntrc();
		
		panelCamposTerrestre.add(new RequiredTextField<>("placa1"));
		panelCamposTerrestre.add(new RequiredTextField<>("placa2"));
		panelCamposTerrestre.add(new RequiredTextField<>("placa3"));
		panelCamposTerrestre.add(new RequiredTextField<>("placa4"));
		
		criaEndAcessoLink();
		
		criaLoginAcesso();
		
		criaSenhaAcesso();
		
		criaDropDown();
		
		add(panelCamposTerrestre,panelCamposFluviais);
		
	}







	private void criaSenhaAcesso() {
		add(new RequiredTextField<>("senhaAcesso"));
	}







	private void criaLoginAcesso() {
		add(new RequiredTextField<>("loginAcesso"));
	}







	private void criaEndAcessoLink() {
		add(new RequiredTextField<>("endAcessoLink"));
	}







	private void criaRntrc() {
		panelCamposTerrestre.add(new RequiredTextField<>("rntrc"));
	}







	private void criaNuEmbarcacao() {
		panelCamposFluviais.add(new RequiredTextField<>("nuEmbarcacao"));
	}
	
	
	
	



	private void criaDropDown() {
		IChoiceRenderer<DominioTipoVeiculo> renderer = new ChoiceRenderer<DominioTipoVeiculo>("descricao");
		final DropDownChoice<DominioTipoVeiculo> dropDownVeiculo = new DropDownChoice<DominioTipoVeiculo>("tpVeiculo",tipoveiculoService.buscarTodos(),renderer);
		
		
		dropDownVeiculo.add(new AjaxFormComponentUpdatingBehavior("change") {
		
			private static final long serialVersionUID = 1L;

			@Override
			protected void onUpdate(AjaxRequestTarget target) {
			
				DominioTipoVeiculo tipo = dropDownVeiculo.getModelObject();
				
				boolean isTerrestre = tipo.getCodigo().equals(DominioTipoVeiculo.COD_TERRESTRE);
				
				panelCamposTerrestre.setVisible(isTerrestre);
				panelCamposFluviais.setVisible(!isTerrestre);
				
				target.add(panelCamposFluviais,panelCamposTerrestre);
			}
		});
		add(dropDownVeiculo);
	}

	private void criaPanelFluvial() {
		panelCamposFluviais = new WebMarkupContainer("panelFluviais");
		panelCamposFluviais.setVisible(false);
		panelCamposFluviais.setOutputMarkupPlaceholderTag(true);
	}

	private void criaPanelTerrestre() {
		panelCamposTerrestre = new WebMarkupContainer("panelTerrestres");
		panelCamposTerrestre.setVisible(false);
		panelCamposTerrestre.setOutputMarkupPlaceholderTag(true);
	}


	
	@Override
	protected void onSubmit() {
		Veiculo veiculo = (Veiculo)getDefaultModelObject();
		veiculoService.salvar(veiculo);
	}

	private void criaVoltar() {
		add(new Link("voltar"){
		
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(ConsultarVeiculoIndexPage.class);
			}
		});
	}

}
